<?php 
class Weibos 
{
	 const WB_AKEY = '1938875617';
	 const WB_SKEY = '22ba789b6827612e2da23905941583c9';
	 const QQ_AKEY = 'e4b02d0dde84400e87065d3975817d33';
	 const QQ_SKEY = '270095dc054af048c84ace2ca19662e4';
	 public static function check_login()
	 {
	 	$id = $_SESSION['id'];
	 	if(empty($id))
	 	{
	 		return false;
	 	}
	 	else
	 	{
	 		//缓存到数据库中先
	 		//查询数据库是否有uid
	 		$user_E = Users::getByid($id);
	 		
	 		
	 		return $user_E; 
	 	}
	 }
	 public static function get_Stoken($callback)
	 {
	 	$o = new WeiboOAuth( self::WB_AKEY , self::WB_SKEY  );
		$keys = $o->getRequestToken();
		$aurl = $o->getAuthorizeURL( $keys['oauth_token'] ,false , $callback);
		$_SESSION['keys'] = $keys;
		return $aurl;
	 }
 	 public static function get_Qtoken($callback)
	 {
	 	$o = new MBOpenTOAuth( self::QQ_AKEY , self::QQ_SKEY  );
		$keys = $o->getRequestToken($callback);
		$aurl = $o->getAuthorizeURL( $keys['oauth_token']  ,false,'');
		$_SESSION['keys'] = $keys;
		return $aurl;
	 }
     public static function get_Qauth($request)
	 {
		$oauth_verifier = $request->get('oauth_verifier');
		$o = new MBOpenTOAuth( self::QQ_AKEY , self::QQ_SKEY  , $_SESSION['keys']['oauth_token'] , $_SESSION['keys']['oauth_token_secret']  );
	
		$last_key = $o->getAccessToken( $oauth_verifier ) ;
		
		$_SESSION['last_key'] = $last_key;
		$qq_client = new MBApiClient(self::QQ_AKEY , self::QQ_SKEY , $last_key['oauth_token'] , $last_key['oauth_token_secret']);
		$ret = $qq_client->getUserInfo();
		if(!empty($last_key['error_code']))
		{
			echo "request sina weibo intrerface error, please re try!";
			echo "<a href='/token'>返回</a>";
			exit;
		}
		$uid = $last_key['name'];
		$quser_E = Qusers::getByUid($uid);
		if(empty($quser_E) || time() - $quser_E->mtime   > 86400)
 		{
 			$qq_client = new MBApiClient(self::QQ_AKEY , self::QQ_SKEY , $last_key['oauth_token'] , $last_key['oauth_token_secret']);
			$qq_ret = $qq_client->getUserInfo();
			$qq_user = $qq_ret['data'];
 			//格式化sina_user 数据
 			if(!empty($quser_E))
 			{
 				//update user数据
 				$quser_E->uid = $qq_user['name'];
 				$quser_E->name = $qq_user['name'];
 				$quser_E->nick = $qq_user['nick'];
 				
 				$quser_E->birth_day = $qq_user['birth_day'];
 				$quser_E->birth_month = $qq_user['birth_month'];
 				$quser_E->birth_year = $qq_user['birth_year'];
 				
 				$quser_E->country_code = $qq_user['country_code'];
 				$quser_E->province_code = $qq_user['province_code'];
 				$quser_E->city_code = $qq_user['city_code'];
 				
 				
 				$quser_E->location = $qq_user['location'];
 				$quser_E->verifyinfo = $qq_user['verifyinfo'];
 				$quser_E->introduction = $qq_user['introduction'];
 				
 				$quser_E->head = $qq_user['head'];
 				$quser_E->sex = $qq_user['sex'];
 				
 				$quser_E->fansnum = $qq_user['fansnum'];
 				$quser_E->idolnum = $qq_user['idolnum'];
 				$quser_E->isent = $qq_user['isent'];
 				$quser_E->isvip = $qq_user['isvip'];
 				$quser_E->tweetnum = $qq_user['tweetnum'];
 				$quser_E->tag = serialize($qq_user['tag']);
 				$quser_E->edu = serialize($qq_user['edu']);
 				$quser_E->token = $last_key['oauth_token'];
 				$quser_E->token_secret = $last_key['oauth_token_secret'];
 				$quser_E->mtime = time();
 				
 				
 				$user_E = Users::getByIid($quser_E->id);
 				
 			}
 			else
 			{
 				//insert user数据
 				$row['uid'] = $qq_user['name'];
 				$row['name'] = $qq_user['name'];
 				$row['nick'] = $qq_user['nick'];
 				
 				$row['birth_day'] = $qq_user['birth_day'];
 				$row['birth_month'] = $qq_user['birth_month'];
 				$row['birth_year'] = $qq_user['birth_year'];
 				
 				$row['country_code'] = $qq_user['country_code'];
 				$row['province_code'] = $qq_user['province_code'];
 				$row['city_code'] = $qq_user['city_code'];
 				
 				
 				$row['location'] = $qq_user['location'];
 				$row['verifyinfo'] = $qq_user['verifyinfo'];
 				$row['introduction'] = $qq_user['introduction'];
 				
 				$row['head'] = $qq_user['head'];
 				$row['sex'] = $qq_user['sex'];
 				
 				$row['fansnum'] = $qq_user['fansnum'];
 				$row['idolnum'] = $qq_user['idolnum'];
 				$row['isent'] = $qq_user['isent'];
 				$row['isvip'] = $qq_user['isvip'];
 				$row['tweetnum'] = $qq_user['tweetnum'];
 				$row['tag'] = serialize($qq_user['tag']);
 				$row['edu'] = serialize($qq_user['edu']);
 				$row['token'] = $last_key['oauth_token'];
 				$row['token_secret'] = $last_key['oauth_token_secret'];
 				
 				$quser_E = Qusers::insertEntity($row);
 				$info['iid'] = $quser_E->id;
 				$info['sign'] = 2;
 				$user_E = Users::insertEntity($info);
 			}
 			
 		}
		else 
		{
			$user_E = Users::getByIid($quser_E->id);
		}
		$_SESSION['sign'] = 2;
 		$_SESSION['id'] = $user_E->id;
	 }
	 public static function get_Sauth($request)
	 {
		$oauth_verifier = $request->get('oauth_verifier');
		$o = new WeiboOAuth( self::WB_AKEY , self::WB_SKEY  , $_SESSION['keys']['oauth_token'] , $_SESSION['keys']['oauth_token_secret']  );

		$last_key = $o->getAccessToken(  $oauth_verifier ) ;
		if(!empty($last_key['error_code']))
		{
			echo "request sina weibo intrerface error, please re try!";
			echo "<a href='/token'>返回</a>";
			exit;
		}
		
		$_SESSION['last_key'] = $last_key;
	 	$uid = $last_key['user_id'];
	 	$suser_E = Susers::getByUid($uid);
		if(empty($suser_E) || time() - $suser_E->mtime   > 86400)
 		{
 			$weibo_client = new WeiboClient(self::WB_AKEY , self::WB_SKEY , $last_key['oauth_token'] , $last_key['oauth_token_secret']  );
 			$sina_user = $weibo_client->show_user($uid);
	 		if(!empty($sina_user['error_code']))
			{
				echo "request sina weibo intrerface error, please re try!";
				echo "<a href='/token'>返回</a>";
				exit;
			}
 			//格式化sina_user 数据
 			if(!empty($suser_E))
 			{
 				//update user数据
 				$suser_E->screen_name = $sina_user['screen_name'];
 				$suser_E->name = $sina_user['name'];
 				$suser_E->province = $sina_user['province'];
 				$suser_E->city = $sina_user['city'];
 				$suser_E->location = $sina_user['location'];
 				$suser_E->url = $sina_user['url'];
 				$suser_E->profile_image_url = $sina_user['profile_image_url'];
 				if( $sina_user['gender'] == 'm')
 				{
 					$gender = 1;
 				}
 				else 
 				{
 					$gender = 0;
 				}
 				$suser_E->gender = $gender;
 				$suser_E->followers_count = $sina_user['followers_count'];
 				$suser_E->friends_count = $sina_user['friends_count'];
 				$suser_E->statuses_count = $sina_user['statuses_count'];
 				$suser_E->favourites_count = $sina_user['favourites_count'];
 				
 				$created_at = strtotime($sina_user['created_at']);
 				$suser_E->created_at = $created_at;
 				$suser_E->following = $sina_user['following'];
 				$suser_E->allow_all_act_msg = $sina_user['allow_all_act_msg'];
 				$suser_E->geo_enabled = $sina_user['geo_enabled'];
 				$suser_E->verified = $sina_user['verified'];
 				$suser_E->token = $last_key['oauth_token'];
 				$suser_E->token_secret = $last_key['oauth_token_secret'];
 				$suser_E->mtime = time();
 				
 				$user_E = Users::getByIid($suser_E->id);
 				
 			}
 			else
 			{
 				//insert user数据
 				
 				$row['uid'] = $sina_user['id'];
 				$row['screen_name'] = $sina_user['screen_name'];
 				$row['name'] = $sina_user['name'];
 				$row['province'] = $sina_user['province'];
 				$row['city'] = $sina_user['city'];
 				$row['location'] = $sina_user['location'];
 				$row['url'] = $sina_user['url'];
 				$row['profile_image_url'] = $sina_user['profile_image_url'];
 				if( $sina_user['gender'] == 'm')
 				{
 					$gender = 1;
 				}
 				else 
 				{
 					$gender = 0;
 				}
 				$row['gender'] = $gender;
 				$row['followers_count'] = $sina_user['followers_count'];
 				$row['friends_count'] = $sina_user['friends_count'];
 				$row['statuses_count'] = $sina_user['statuses_count'];
 				$row['favourites_count'] = $sina_user['favourites_count'];
 				
 				$created_at = strtotime($sina_user['created_at']);
 				$row['created_at'] = $created_at;
 				$row['following'] = $sina_user['following'];
 				$row['allow_all_act_msg'] = $sina_user['allow_all_act_msg'];
 				$row['geo_enabled'] = $sina_user['geo_enabled'];
 				$row['verified'] = $sina_user['verified'];
 				
 				$row['token'] = $last_key['oauth_token'];
 				$row['token_secret'] = $last_key['oauth_token_secret'];
 				
 				$suser_E = Susers::insertEntity($row);
 				$info['iid'] = $suser_E->id;
 				$info['sign'] = 1;
 				$user_E = Users::insertEntity($info);
 			}
 			
 		}
		else 
		{
			$user_E = Users::getByIid($suser_E->id);
		}
		$_SESSION['sign'] = 1;
 		$_SESSION['id'] = $user_E->id;
	 }
	 public static function Follow($id)
	 {
	 	$user_E = Users::getById($id);
	 	$uid = $user_E->getUid();
	 	if($_SESSION['sign'] == '1')
	 	{
		 	$client = new WeiboClient(self::WB_AKEY, self::WB_SKEY, $_SESSION['last_key']['oauth_token'], $_SESSION['last_key']['oauth_token_secret']);
		 	$client->follow($uid);
	 	}
	 	
	 	else if($_SESSION['sign'] == '2')
	 	{
 			$qq_client = new MBApiClient(self::QQ_AKEY , self::QQ_SKEY , $_SESSION['last_key']['oauth_token'], $_SESSION['last_key']['oauth_token_secret']);
			
			
			$p = array(
				'n' => $uid,
				'type' => 1
			);
			$ret = $qq_client->setMyidol($p);
	 	}
	 	
	 }
	 public static function unFollow($id)
	 {
	 	$user_E = Users::getById($id);
	 	$uid = $user_E->getUid();
	 	if($_SESSION['sign'] == '1')
	 	{
		 	$client = new WeiboClient(self::WB_AKEY, self::WB_SKEY, $_SESSION['last_key']['oauth_token'], $_SESSION['last_key']['oauth_token_secret']);
		 	$client->unfollow($uid);
	 	}
	 	else if($_SESSION['sign'] == '2')
	 	{
 			$qq_client = new MBApiClient(self::QQ_AKEY , self::QQ_SKEY , $_SESSION['last_key']['oauth_token'], $_SESSION['last_key']['oauth_token_secret']);
			
			
			$p = array(
				'n' => $uid,
				'type' => 0
			);
			$ret = $qq_client->setMyidol($p);
	 	}
	 }
	 public static function isFollow($id)
	 {
	 	
	 	$user_E = Users::getById($id);
	 	$uid = $user_E->getUid();
	 	if($_SESSION['sign'] == '1')
	 	{
		 	$client = new WeiboClient(self::WB_AKEY, self::WB_SKEY, $_SESSION['last_key']['oauth_token'], $_SESSION['last_key']['oauth_token_secret']);
		 	$isfollowed = $client->is_followed($uid);
		 	return $isfollowed['target']['followed_by'];
	 	}
	 	else if($_SESSION['sign'] == '2')
	 	{
 			$qq_client = new MBApiClient(self::QQ_AKEY , self::QQ_SKEY , $_SESSION['last_key']['oauth_token'], $_SESSION['last_key']['oauth_token_secret']);
			
			
			$p = array(
				'n' => $uid,
				'type' => 1
			);
			$ret = $qq_client->checkFriend($p);
			return $ret['data'][$uid];
	 	}
	 }
	 public static function upload($context, $pic)
	 {
	 	if($_SESSION['sign'] == '1')
	 	{
		 	$client = new WeiboClient(self::WB_AKEY, self::WB_SKEY, $_SESSION['last_key']['oauth_token'], $_SESSION['last_key']['oauth_token_secret']);
		 	$r = $client->upload(htmlspecialchars_decode($context), $pic);
	 	}
	 	else if($_SESSION['sign'] == '2')
	 	{
 			
			$ip = '192.168.2.1';
			$type=getimagesize($pic);
			$img_type=$type[mime];
			$img = array($img_type,'weixiaozhan6.png',file_get_contents($pic));
			$p =array(
				'c' => $context,
				'ip' => $ip, 
				'j' => '',
				'w' => '',
				'type'=>1,
				'p'=>$img,
			
			);		
 			$qq_client = new MBApiClient(self::QQ_AKEY , self::QQ_SKEY , $_SESSION['last_key']['oauth_token'], $_SESSION['last_key']['oauth_token_secret']);
			$qq_ret = $qq_client->postOne($p);
	 	}
	 }
	public static function update($context)
	 {
	 	if($_SESSION['sign'] == '1')
	 	{
		 	$client = new WeiboClient(self::WB_AKEY, self::WB_SKEY, $_SESSION['last_key']['oauth_token'], $_SESSION['last_key']['oauth_token_secret']);
		 	$r = $client->update(htmlspecialchars_decode($context));
	 	}
	 	else if($_SESSION['sign'] == '2')
	 	{
			$ip = $_SERVER['REMOTE_ADDR'];
			$p =array(
				'c' => $context,
				'ip' => $ip, 
				'j' => '',
				'w' => '',
			);		
 			$qq_client = new MBApiClient(self::QQ_AKEY , self::QQ_SKEY , $_SESSION['last_key']['oauth_token'], $_SESSION['last_key']['oauth_token_secret']);
			$qq_ret = $qq_client->postOne($p);
	 	}
	 }
	 public static function followers($id)
	 {
	 	$user_E = Users::getById($id);
	 	$uid = $user_E->getUid();
	 	if($user_E->sign == '1')
	 	{
		 	$client = new WeiboClient(self::WB_AKEY, self::WB_SKEY, '9d78d197d527157099ac3d0e0643675e', '3c2287430487daab7729eff670876958');
		 	$ret = $client->followers(false,12,$uid);
		 	foreach($ret as $k => $v)
		 	{
		 		$followers[] = array('href'=>"http://t.sina.com.cn/".$v['id'], 'name'=>$v['name'], 'image'=>$v['profile_image_url']);
		 	}
		 	return $followers;
	 	}
	 	else if($user_E->sign == '2')
	 	{
	 		$qq_client = new MBApiClient(self::QQ_AKEY , self::QQ_SKEY , '15c942471aeb4f70a29a7b32b66da6d5' , '34810d4eb9e0290fe5717f6eb924b0aa');
			$p = array(
				'n' => $uid,
				'num' => 12,
				'start' => 0,
				'type' => 0
			);
			$ret = $qq_client->getfans($p);
			
	 		foreach($ret['data']['info'] as $k => $v)
		 	{
		 		if(empty($v['head']))
		 		{
		 			$head = "/pics/site/head_50.jpg";
		 		}
		 		else 
		 		{
		 			$head = $v['head']."/50";
		 		}
		 		$followers[] = array('href'=>"http://t.qq.com/".$v['name'], 'name'=>$v['name'], 'image'=>$head);
		 	}
			return $followers;
	 	}
	 }
	 public static function end_session()
	 {
	 	$client = new WeiboClient(self::WB_AKEY, self::WB_SKEY, 'f9aa4dd44b4295ea480d51eec9b28db1', '65a4cc7bb7b572f9f34020bd75618414');
	 	$r = $client->end_session();
	 	return $r;
	 }
	 
}
?>